var mongoose = require('mongoose');
var CateType=require("../models/mod_cateType");


//创建根分类
exports.catetype_create=function(req, res, next) {
    const catetype = new CateType(req.body);
    catetype.save().then((data) => {
        res.json(data);
    });
};

//修改根分类
exports.catetype_update=function(req, res, next) {
    const id=req.params.id;
    CateType.findByIdAndUpdate(id,{$set:req.body},{new:true}).then(data=>{
        res.json(data);
    });
};

//删除根分类
exports.catetype_delete=function(req, res, next) {
    const id=req.params.id;
    CateType.findByIdAndDelete(id).then(data=>{
        res.json(data);
    });
};
// exports.remove=function(req, res, next) {
//     const id=req.body.md;
//     var as=id.split(",");
//     if(as.length>0){
//         User.remove({_id:{$in:as}}).then(data=>{
//             res.json(data);
//         });
//     }
    
//     // console.log(as)
// };

//查询单个根分类
exports.catetype_get=function(req, res, next) {
    const id=req.params.id;
    CateType.findById(id).then(data=>{
        res.json(data);
    });
};


exports.catetype_list=function(req, res, next) {
    var page = req.body.page ? req.body.page : 1;
    var rows = req.body.rows ? req.body.rows : 10;
    var search = req.body.search ? req.body.search : '';

    var options = {
        page: +page,
        limit: +rows,
        sort: { _id: -1 }
    }

    var queryparams={};
    if(req.body.search&&search.length>0){
        queryparams={
            "name":new RegExp(search,"i")
        }
    }

    CateType.paginate(queryparams, options, function (err, result) {
        result.rows = result.docs;
        delete result.docs;
        res.json(result);
    });
};